package DemoKoneksi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class DemoKoneksi {
	public static void main(String[] args) throws Exception{
		String dbDriver ="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://localhost/belajar";
		String user = "root";
		String pass = "";
		String sql = "Select * FROM produk";
		
		//register database
		Class.forName(dbDriver);
		
		//buka koneksi
		Connection c = DriverManager.getConnection(url, user, pass);
		
		//jalankan sql
		ResultSet rs = c.createStatement().executeQuery(sql);
		
		//tampilkan resultset
		while(rs.next()){
			System.out.println("ID Produk:" + rs.getInt("id") );
			System.out.println("Kode Produk:" + rs.getString("kode") );
			System.out.println("Nama Produk:" + rs.getString("nama") );
			System.out.println("Nama Produk:" + rs.getDouble("harga"));
		}
		
		//tutup koneksi
		rs.close();
		c.close();
	}
}
